<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.0.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <link rel="alternate" href="/atom.xml" title="刘攀的个人专栏" type="application/atom+xml">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    hostname: new URL('http://liupan.run').hostname,
    root: '/',
    scheme: 'Pisces',
    version: '7.5.0',
    exturl: false,
    sidebar: {"position":"right","Pisces | Gemini":240,"display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: 'search.xml',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="在网页中使用加载进度条效果">
<meta name="keywords" content="进度条">
<meta property="og:type" content="article">
<meta property="og:title" content="018-网页中使用进度条">
<meta property="og:url" content="http:&#x2F;&#x2F;liupan.run&#x2F;2020&#x2F;01&#x2F;03&#x2F;018-%E7%BD%91%E9%A1%B5%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%BF%9B%E5%BA%A6%E6%9D%A1&#x2F;index.html">
<meta property="og:site_name" content="刘攀的个人专栏">
<meta property="og:description" content="在网页中使用加载进度条效果">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-01-03T14:54:42.167Z">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://liupan.run/2020/01/03/018-%E7%BD%91%E9%A1%B5%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%BF%9B%E5%BA%A6%E6%9D%A1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>018-网页中使用进度条 | 刘攀的个人专栏</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">刘攀的个人专栏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">面朝大海,春暖花开</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">28</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">0</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">20</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="popup search-popup">
    <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="none"
           placeholder="搜索..." spellcheck="false"
           type="text" id="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result"></div>

</div>
<div class="search-pop-overlay"></div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://liupan.run/2020/01/03/018-%E7%BD%91%E9%A1%B5%E4%B8%AD%E4%BD%BF%E7%94%A8%E8%BF%9B%E5%BA%A6%E6%9D%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="liupan">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="刘攀的个人专栏">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          018-网页中使用进度条
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-01-03 11:01:01 / 修改时间：22:54:42" itemprop="dateCreated datePublished" datetime="2020-01-03T11:01:01+08:00">2020-01-03</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>在网页中使用加载进度条效果</p>
</blockquote>
<a id="more"></a>

<h3 id="1-定时器进度条（非真实）"><a href="#1-定时器进度条（非真实）" class="headerlink" title="1.定时器进度条（非真实）"></a>1.定时器进度条（非真实）</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&lt;script type=&quot;text/javascript&quot;&gt; </span><br><span class="line">    $(function()&#123; </span><br><span class="line">        var loading=‘&lt;div class=&quot;loading&quot;&gt;&lt;div class=&quot;pic&quot;&gt;&lt;/div&gt;&lt;/div&gt;‘; </span><br><span class="line">        $(&quot;body&quot;).append(loading); </span><br><span class="line">        setInterval(function()&#123; </span><br><span class="line">            $(&quot;.loading&quot;).fadeOut(); </span><br><span class="line">        &#125;,3000); </span><br><span class="line">    &#125;); </span><br><span class="line">&lt;/script&gt;</span><br></pre></td></tr></table></figure>

<h3 id="2-真实进度条"><a href="#2-真实进度条" class="headerlink" title="2.真实进度条"></a>2.真实进度条</h3><p>要实现根据真实内容，来加载进度条，下面要介绍两个知识点：</p>
<p>document.onreadystatechange 页面加载状态改变时的事件<br>document.readyState 返回当前文档的状态</p>
<ol>
<li>uninitialized - 还未开始载入</li>
<li>loading    - 载入中</li>
<li>interactive  - 已加载，文档与用户可以开始交互</li>
<li>complete    - 载入完成</li>
</ol>
<h4 id="2-1修改定时器代码"><a href="#2-1修改定时器代码" class="headerlink" title="2.1修改定时器代码"></a>2.1修改定时器代码</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">document</span>.onreadystatechange=<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123; </span><br><span class="line">         <span class="keyword">if</span>(<span class="built_in">document</span>.readyState==<span class="string">"complete"</span>)&#123; </span><br><span class="line">                  $(<span class="string">".loading"</span>).fadeOut(); </span><br><span class="line">         &#125; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="2-2将进度条变成css的小动画进行显示"><a href="#2-2将进度条变成css的小动画进行显示" class="headerlink" title="2.2将进度条变成css的小动画进行显示"></a>2.2将进度条变成css的小动画进行显示</h4><p> <a href="https://preloaders.net/" target="_blank" rel="noopener">https://preloaders.net/</a> 此网站有各种表示加载的小动画 </p>
<p><a href="http://autoprefixer.github.io/" target="_blank" rel="noopener">http://autoprefixer.github.io/</a> 在线给css添加前缀 </p>
<p><a href="https://loading.io/" target="_blank" rel="noopener">https://loading.io/</a> 进度条小动画 </p>
<h4 id="2-3定位在头部的进度条（按页面执行顺序计算，非真实）"><a href="#2-3定位在头部的进度条（按页面执行顺序计算，非真实）" class="headerlink" title="2.3定位在头部的进度条（按页面执行顺序计算，非真实）"></a>2.3定位在头部的进度条（按页面执行顺序计算，非真实）</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$(<span class="string">".loading .pic"</span>).animate(&#123;<span class="attr">width</span>:<span class="string">"100%;"</span>&#125;,<span class="number">100</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    $(<span class="string">".loading"</span>).fadeOut();</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="2-4实时获取加载进度条"><a href="#2-4实时获取加载进度条" class="headerlink" title="2.4实时获取加载进度条"></a>2.4实时获取加载进度条</h4><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">img</span>=<span class="title">new</span> <span class="title">Image</span>(<span class="params"></span>);</span></span><br><span class="line"><span class="function"><span class="title">img</span>.<span class="title">onload</span>=<span class="title">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line">img.src=<span class="string">'133.jpg'</span>;  <span class="comment">//此src属性必须写在onload后面；</span></span><br></pre></td></tr></table></figure>

<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> img=$(<span class="string">'img'</span>);</span><br><span class="line"><span class="keyword">var</span> num=<span class="number">0</span>;</span><br><span class="line">img.each(<span class="function"><span class="keyword">function</span>(<span class="params">i</span>)</span>&#123;</span><br><span class="line">    <span class="keyword">var</span> oImg=<span class="keyword">new</span> Image();</span><br><span class="line">    oImg.onload=<span class="literal">null</span>;</span><br><span class="line">    num++;</span><br><span class="line">    $(<span class="string">".loading b"</span>).html(<span class="built_in">parseInt</span>(num/$(<span class="string">"img"</span>).size()*<span class="number">100</span>)+<span class="string">"%"</span>);</span><br><span class="line">    <span class="keyword">if</span>(num&gt;=<span class="number">1</span>)&#123;</span><br><span class="line">        $(<span class="string">".loading"</span>).fadeOut();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line">oImg.src=img[i].src;</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%BF%9B%E5%BA%A6%E6%9D%A1/" rel="tag"># 进度条</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/01/03/017-VSCode%E6%8F%92%E4%BB%B6ProjectsManager/" rel="prev" title="017-VSCode插件ProjectsManager">
      <i class="fa fa-chevron-left"></i> 017-VSCode插件ProjectsManager
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/01/03/019-CSS%E4%B8%AD%E5%B8%B8%E7%94%A8%E5%BD%A2%E7%8A%B6/" rel="next" title="019-CSS中常用形状">
      019-CSS中常用形状 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-定时器进度条（非真实）"><span class="nav-text">1.定时器进度条（非真实）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-真实进度条"><span class="nav-text">2.真实进度条</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1修改定时器代码"><span class="nav-text">2.1修改定时器代码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2将进度条变成css的小动画进行显示"><span class="nav-text">2.2将进度条变成css的小动画进行显示</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3定位在头部的进度条（按页面执行顺序计算，非真实）"><span class="nav-text">2.3定位在头部的进度条（按页面执行顺序计算，非真实）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4实时获取加载进度条"><span class="nav-text">2.4实时获取加载进度条</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="liupan"
      src="/images/avatar.gif">
  <p class="site-author-name" itemprop="name">liupan</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">20</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">28</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="feed-link motion-element">
    <a href="/atom.xml" rel="alternate">
      <i class="fa fa-rss"></i>RSS
    </a>
  </div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/facetothesea" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;facetothesea" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="/mailto:hbxgliupan@sina.com" title="E-Mail → mailto:hbxgliupan@sina.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">liupan</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.0.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> v7.5.0
  </div>

        








        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>



  




  <script src="/js/local-search.js"></script>













  

  

  

</body>
</html>
